local function text_new_key(self, key)
	self.text = self.text..key
end

local function text_set_focus(self, focus)
         self.focus = focus
end

img =  canvas:new("img/back.png")
imgf =  canvas:new("img/text.png")
imge =  canvas:new("img/error.png")

print (">>> IMGS OK")

function text_draw (self)
        local text = self.text
        canvas:attrFont('vera', self.h*3/4)
        if self.focus then
	    canvas:compose (self.x-15, self.y-11, imgf) 
	end
	canvas:attrColor('black')
	if self.password then
		text = ""
		for i=1,#self.text do
			text = text.."*"
		end
	else
		text = self.text
	end
	canvas:drawText(self.x+1,self.y+1, text)
	--canvas:flush()
	print (">>>>1 wid draw")
end
local function text_backspace (self)
	self.text = string.sub(self.text, 1, -2)
end

local function text_clear (self)
             self.text = ""
end

local function text_click (self)
end

function TextInput (x, y, h, w, password)
	local tf = {focus = nil, x = x, y = y, h = h , w = w, password=password,  text = '', key_press = text_new_key, draw = text_draw, backspace = text_backspace, clear = text_clear, set_focus = text_set_focus, on_click = text_click}
	return tf
end

local function add_widget(self, nome, widget)
	self.widgets[nome] = widget
	if self.focus == nil then
		self.focus = nome
		widget:set_focus(true)
	end
	self:draw()
end

local function focus_next(self)
	pr = nil
	ac = nil
	--canvas:compose (0, 0, img)
	for k,v in pairs(self.widgets) do
		pr = ac
		ac = k
		if pr == self.focus then
			self.widgets[pr]:set_focus(false)
			v:set_focus(true)	
			--self.widgets[pr]:draw()
			self.focus = k
		end
	end
end

local function focus_prev(self)
	pr = nil
	ac = nil
	for k,v in pairs(self.widgets) do
		pr = ac
		ac = k
		if ac == self.focus and pr then
			v:set_focus(false)
			self.widgets[pr]:set_focus(true)
			self.focus = pr
		end
	end
end

local function get_widget(self)
	if self.focus then
		return self.widgets[self.focus]
	end
end

local function widget_handler (self, evt)
	print (">>>> EVENTO", evt.class, evt.type)
	if evt.class ~= 'key' then return end
	if evt.type ~= 'press' then return true end
	local key = evt.key
	print ("TECLA", key)

	-- SELECT
	if (key == 'ENTER') then
		self:get():on_click()
	-- BACKSPACE
	elseif (key == 'CURSOR_LEFT') then
		self:get():backspace(key)
	elseif (key == 'CURSOR_RIGHT') then
		self:get():clear(key)
	-- UPPER
	elseif (key == 'CURSOR_UP') then
		self:focus_prev()
	-- UPPER
	elseif (key == 'CURSOR_DOWN') then
		self:focus_next()
	-- NUMBER
	elseif _G.tonumber(key) then
		self:get():key_press(key)
	end
	self:draw()
	return true
end


local function widget_draw (self)
	w, h = canvas:attrSize()
	canvas:attrColor(200, 200, 200, 0)
	canvas:drawRect('fill', 0, 0, w, h)
	canvas:compose (0, 0, img)
	for k,v in pairs(self.widgets) do
		v:draw()
	end
	-- FIXME o erro estah ad-hoc
	local function no_error()
		self.dERROR = false
		self:draw()
	end
	if self.ERROR then
		event.timer(2000, no_error)
		self.ERROR = false
		self.dERROR = true
	end
	if self.dERROR then
		canvas:compose (30, 220, imge)
	end
	canvas:flush()
	print (">>>>FLUSH")
end



function WidgetMagager ()
	local wm = {focus = nil, widgets = {}, add = add_widget, focus_next = focus_next, focus_prev = focus_prev, get = get_widget, handle = widget_handler, draw=widget_draw}
	local function handler (evt) wm:handle(evt) end
	event.register(handler)
	print (">>>>Registrado ev")
	wm:draw()
	print (">>>>WM draw")
	return wm
end

function set_attr(attr, value)
	local evt = {
		class = 'ncl',
		type  = 'attribution',
		name  = attr,
		value = value
	}
	evt.action = 'start'; event.post(evt)
	evt.action = 'stop';  event.post(evt)
end

--###################################################

usuarios = {}
usuarios['123'] = {'123', 'Izon Thomaz Mielke'}
usuarios['321'] = {'123', 'Igor Magri Vale'}

--###################################################

login = TextInput(30, 138, 25, 200, false)
senha = TextInput(30, 187, 25, 200, true)

wm = WidgetMagager ()
wm:add('login', login)
wm:add('senha', senha)
wm:draw()


print (">>>>WM OK")

function login:on_click()
	wm:focus_next()
end

function senha:on_click()
	local user = usuarios[login.text]
	if true or (user and user[1] == self.text) then
		print ("Sucesso")
		--FIXME
		set_attr("loginstatus", "true")
	else
		print ("Falha")
		wm.ERROR=true
		self:clear()
		wm:focus_prev()
	end
end

--###################################################
